package ru.susu.algebra.centralunits.alternating.tex.local;

import java.math.BigInteger;

import ru.susu.algebra.centralunits.alternating.MathMethodWithInitializers;
import ru.susu.algebra.centralunits.alternating.initializers.QuadraticFieldsInitializer;
import ru.susu.algebra.centralunits.alternating.initializers.SpecialRowsInitializer;
import ru.susu.algebra.field.quadratic.QuadraticField;
import ru.susu.algebra.field.quadratic.QuadraticFieldGeneralForm;
import ru.susu.algebra.field.quadratic.QuadraticInteger;
import ru.susu.algebra.jtex.ITexElement;
import ru.susu.algebra.jtex.SimpleTexElementWithCode;
import ru.susu.algebra.jtex.StringTexElement;
import ru.susu.algebra.jtex.TexBeginEndElement;
import ru.susu.algebra.jtex.UnionTexElement;
import ru.susu.algebra.jtex.formula.FormulaTexElement;
import ru.susu.algebra.jtex.formula.GreekSymbols;
import ru.susu.algebra.jtex.formula.MathSymbols;
import ru.susu.algebra.jtex.utils.TexUtils;
import ru.susu.algebra.partition.Partition;
import ru.susu.algebra.properties.IPropertySource;

/**
 * @author akargapolov
 * @since: 06.09.2010
 */
public class NotIntegerCharactersLemma extends MathMethodWithInitializers<ITexElement>
{
	private static final Class[] DEPENDENCIES = {SpecialRowsInitializer.class, QuadraticFieldsInitializer.class};

	public static String getLabelName(Partition partition, IPropertySource ps)
	{
		Integer number = SpecialRowsInitializer.getRowNumber(ps, partition);
		return "om" + number;
	}

	private ITexElement getLemma(Partition partition, IPropertySource ps) throws Exception
	{
		Integer rowNumber = SpecialRowsInitializer.getRowNumber(ps, partition);
		QuadraticField field = QuadraticFieldsInitializer.getRow2Field(ps).get(partition);
		QuadraticInteger generateInteger = field.getGenerateInteger();
		QuadraticInteger character = field.getGenerateInteger();
		String aNumber = "A" + TexUtils.index(rowNumber);
		String doubleSlash = "," + MathSymbols.DOUBLE_INV_SLASH + "\n";
		BigInteger sqrtNumber = field.getGenerateInteger().getD();
		String omegaNumber = GreekSymbols.OMEGA + TexUtils.index(sqrtNumber);
		QuadraticFieldGeneralForm omega = new QuadraticFieldGeneralForm(BigInteger.ZERO, BigInteger.ONE, character.getD());
		QuadraticFieldGeneralForm conjugateOmega = (QuadraticFieldGeneralForm)omega.getConjugateNumber();
		QuadraticFieldGeneralForm omega2 = (QuadraticFieldGeneralForm)omega.pow(BigInteger.valueOf(2));

		TexBeginEndElement lemma = TexBeginEndElement.lemma();
		lemma.addSubElement(SimpleTexElementWithCode.label(getLabelName(partition, ps)));
		lemma.addSubElement(StringTexElement.text("Для $" + generateInteger.getBDInSqrt() + "$\n"));

		lemma.addSubElement(TexBeginEndElement.align().addSubElement(StringTexElement.text(
				aNumber + " = " + TexUtils.toTexString(character) + doubleSlash +
				"*" + aNumber + " = " + TexUtils.toTexString(character.getConjugateNumber()) + doubleSlash +
				omegaNumber + " = " + FormulaTexElement.frac("1 + " + FormulaTexElement.sqrt(sqrtNumber), "2") + doubleSlash +
				omegaNumber + TexUtils.pow("2") + " = " + TexUtils.toTexString(omega.pow(BigInteger.valueOf(2))) + doubleSlash +
				omegaNumber + TexUtils.pow("*") + " = " + TexUtils.toTexString(conjugateOmega) + doubleSlash +
				MathSymbols.TR + omegaNumber + " = " + omega.getTrace() + doubleSlash +
				MathSymbols.TR + omegaNumber + TexUtils.pow("*") + " = " + conjugateOmega.getTrace() + doubleSlash +
				MathSymbols.TR + omegaNumber + TexUtils.pow("2") + " = " + omega2.getTrace() + doubleSlash +
				omegaNumber + omegaNumber +TexUtils.pow("*") + " = " + TexUtils.toTexString(omega.multiply(conjugateOmega)) + ".")));

		return new UnionTexElement().addSubElement(lemma).addSubElement(StringTexElement.newLine());
	}

	@Override
	protected ITexElement directRun(IPropertySource ps) throws Exception
	{
		UnionTexElement union = new UnionTexElement();

		union.addSubElement(StringTexElement.text("Следующие леммы очевидны: \n"));

		for (Partition partition : SpecialRowsInitializer.listSpecialNotIntegerCharacterRows(ps))
		{
			union.addSubElement(getLemma(partition, ps));
		}

		return union;
	}

	@Override
	protected Class[] getDependentInitializers()
	{
		return DEPENDENCIES;
	}

	/* Не привожу доказательство
	\begin{lemma}\label{om}
	Имеем для $13$
	\begin{align}
	A=(1+ER(13))/2=1+b13\\
	\omega_{13}=\frac{1+\sqrt{13}}{2}\\
	\omega_{13}^2=3+\omega_{13},\\
	\omega_{13}^*=1-\omega_{13},\\
	\tr\omega_{13}=\tr\omega_{13}^*=1,\\
	\tr\omega_{13}^2=7,\\
	\omega_{13}\omega_{13}^*=-3,\\
	\texttt{A}=\omega_{13},\\
	\texttt{*A}=1-\omega_{13}.
	\end{align}
	\end{lemma}
	\begin{proof}
	В самом деле
	\begin{align*}
	\omega_{13}^2&=\frac{1+13+2\sqrt{13}}{4}=\frac{7+\sqrt{13}}{2}=3+\frac{1+\sqrt{13}}{2}=3+\omega_{13},\\
	\omega_{13}^*&=\frac{1-\sqrt{13}}{2}=1-\omega_{13},\\
	\tr\omega_{13}&=\tr\omega_{13}^*=1,\\
	\tr\omega_{13}^2&=\tr(3+\omega_{13})=3\cdot2+\frac{1+\sqrt{13}}{2}+\frac{1-\sqrt{13}}{2}=6+1=7,\\
	\omega_{13}\omega_{13}^*&=\omega_{13}(1-\omega_{13})=\omega_{13}-\omega_{13}^{2}=\omega_{13}-(3+\omega_{13})=-3,\\
	\texttt{A}&=\frac{1+\sqrt{13}}{2}=\omega_{13},\\
	\texttt{*A}&=\frac{1-\sqrt{13}}{2}=\omega_{13}^*=1-\omega_{13}.
	\end{align*}
	\end{proof}
	*/

}
